package caro.app.business;

/**
 * Class Board
 * @author Thanh Nhan
 * @Since Aug 14, 2011
 * @Version 1.0
 */
public class Board {
    private static final int ROWS = 15;
    private static final int COLUMNS = 15;
    private static final int NUM_WIN = 5;
    public static final char WIN_ROW = 'R';
    public static final char WIN_COLUMN = 'C';
    public static final char WIN_C1 = '1';
    public static final char WIN_C2 = '2';
    public static final char NOT_WIN = 'K';
    private int[][] data;
    public Board(){
        data = new int[ROWS][COLUMNS];
        reset();
    }
    public void reset(){
        for(int i = 0; i < ROWS ; i++)
            for(int j = 0; j < COLUMNS ; j++)
                data[i][j] = -2;
    }
    public void check(int r, int c, int val){
        if(isCellValidate(r, c))
            data[r][c] = val;
    }
    public void unCheck(int r, int c){
        if(isCellValidate(r, c))
            data[r][c] = -2;
    }
    public int getValueAt(int r, int c){
        if(isCellValidate(r, c))
            return data[r][c];
        return -1;
    }
    private boolean isCellValidate(int r, int c){
        return (r >= 0 && c >= 0 && r < ROWS && c < COLUMNS);
    }
    /*
     * Dong chua cell r,c co n o lien tiep duoc check cung gia tri khong
     */
    public int testRow(int r, int c, int numberElement){
        int at = getValueAt(r, c);
        int ci = c;
        int num = 0;
        //Qua trai
        while(ci >= 0 && getValueAt(r, ci) == at){
            num++;
            ci--;
        }
        //Qua phai
        ci = c + 1;
        while(ci < COLUMNS && getValueAt(r, ci) == at){
            num++;
            ci++;
        }
        return (numberElement == num ? ci - c : -1);
    }
    /*
     * Cot chua cell r,c co n o lien tiep duoc check cung gia tri khong
     */
    public int testColumn(int r, int c, int numberElement){
        int at = getValueAt(r, c);
        int ri = r;
        int num = 0;
        //Len tren
        while(ri >= 0 && getValueAt(ri, c) == at){
            num++;
            ri--;
        }
        //Xuong duoi
        ri = r + 1;
        while(ri < ROWS && getValueAt(ri, c) == at){
            num++;
            ri++;
        }
        return (numberElement == num ? ri - r : -1);
    }
    /*
     * Hang cheo chinh chua cell r,c co n o lien tiep duoc check cung gia tri khong
     */
    public int testCheo1(int r, int c, int numberElement){
        int at = getValueAt(r, c);
        int ri = r;
        int ci = c;
        int num = 0;
        //Len tren
        while(ri >= 0 && ci >=0 && getValueAt(ri, ci) == at){
            num++;
            ri--;
            ci--;
        }
        //Xuong duoi
        ri = r + 1;
        ci = c + 1;
        while(ri < ROWS && ci < COLUMNS && getValueAt(ri, ci) == at){
            num++;
            ri++;
            ci++;
        }
        return (numberElement == num ? ri - r : -1);
    }
    /*
     * Hang cheo phu chua cell r,c co n o lien tiep duoc check cung gia tri khong
     */
    public int testCheo2(int r, int c, int numberElement){
        int at = getValueAt(r, c);
        int ri = r;
        int ci = c;
        int num = 0;
        //Len tren
        while(ri >= 0 && ci < COLUMNS && getValueAt(ri, ci) == at){
            num++;
            ri--;
            ci++;
        }
        //Xuong duoi
        ri = r + 1;
        ci = c - 1;
        while(ri < ROWS && ci >= 0 && getValueAt(ri, ci) == at){
            num++;
            ri++;
            ci--;
        }
        return (numberElement == num ? ri - r : -1);
    }
    public char testWin(int r, int c){
        if(testRow(r, c, NUM_WIN) != -1) return WIN_ROW;
        if(testColumn(r, c, NUM_WIN) != -1) return WIN_COLUMN;
        if(testCheo1(r, c, NUM_WIN) != -1) return WIN_C1;
        if(testCheo2(r, c, NUM_WIN) != -1) return WIN_C2;
        return NOT_WIN;
    }
    //Tra ve cell dau tien trong day o thang
    public int[] getFirstCellWin(int row,int column){
        int vl=getValueAt(row, column);
        int i=row,j=column;
        char c=testWin(row,column);
        switch(c){
            case WIN_ROW:
                while(getValueAt(i, j) ==vl) j--;
                j++;
                break;
            case WIN_COLUMN:
                while(getValueAt(i, j)==vl) i--;
                i++;
                break;
            case WIN_C1:
                while(getValueAt(i, j)==vl) {i--; j--;}
                i++; j++;
                break;
            case WIN_C2:
                while(getValueAt(i, j)==vl) {i--; j++;}
                i++; j--;
                break;
            default:
                break;
        }
        int rs[]={i,j};
        return rs;
    }
}
